package org.zzh.service.company;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zzh.dao.common.PageBean;
import org.zzh.dao.mapper.CompanyMapper;
import org.zzh.domain.company.SsCompany;

import java.util.List;
import java.util.UUID;

/**
 * @author 十里坡
 * @description 企业管理服务层实现
 */
@Service
public class SsCompanyServiceImpl implements SsCompanyService {

    @Autowired
    CompanyMapper companyMapper;

    @Override
    public PageBean<SsCompany> list(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<SsCompany> list = companyMapper.list(name);
        PageInfo<SsCompany> ssCompanyPageInfo = new PageInfo<>(list);
        PageBean<SsCompany> pageBean = new PageBean<>();
        pageBean.setData(ssCompanyPageInfo.getList());
        pageBean.setTotalCount((int) ssCompanyPageInfo.getTotal());
        pageBean.setTotalPage(ssCompanyPageInfo.getPages());
        pageBean.setPage(page);
        pageBean.setPageSize(pageSize);
        return pageBean;
    }

    @Override
    @Transactional
    public Boolean add(SsCompany company) {
        companyMapper.add(company);
        if (company.getId() != null) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void deleteList(String[] ids) {
        companyMapper.deleteList(ids);
    }

    @Override
    @Transactional
    public void edit(SsCompany company) {
        if (company.getId() != null && !company.getId().isEmpty()) {
            companyMapper.edit(company);
            return;
        }
        company.setId(UUID.randomUUID().toString());
        companyMapper.add(company);
    }

    @Override
    public SsCompany findById(String id) {
        SsCompany company = companyMapper.findById(id);
        return company;
    }
}
